package com.onesports.intelligent.k12.polarlight.service.course;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.ClassInfo;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.SpellingClassPeriod;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseScheduleDetailVO;
import com.onesports.intelligent.k12.polarlight.enums.ScheduleDetailStatusEnum;
import com.onesports.intelligent.k12.polarlight.enums.SpellingClassPeriodEnum;
import com.onesports.intelligent.k12.polarlight.mapper.course.SpellingClassPeriodMapper;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author Wsy
 **/
@Service
@RequiredArgsConstructor
public class SpellingClassPeriodService extends ServiceImpl<SpellingClassPeriodMapper, SpellingClassPeriod> {

    private final SpellingClassPeriodMapper spellingClassPeriodMapper;


    /**
     * 获取最早的未过期的课
     *
     * @param classId 班级id
     * @return SpellingClassPeriodEnum
     */
    public SpellingClassPeriod getEarliestCurrentPeriod(String classId, Date currentDate) {
        List<SpellingClassPeriod> list = this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, classId)
                .eq(SpellingClassPeriod::getStatus, SpellingClassPeriodEnum.NO_EXPIRED.getValue())
                .gt(SpellingClassPeriod::getCourseDate, currentDate)
                .orderByAsc(SpellingClassPeriod::getCourseDate));
        if (ParamUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }


    /**
     * 获取最早的未过期的课
     *
     * @param classId 班级id
     * @return SpellingClassPeriodEnum
     */
    public SpellingClassPeriod getEarliestCurrentCanUsePeriod(String classId, Date currentDate) {
        List<SpellingClassPeriod> list = this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, classId)
                .eq(SpellingClassPeriod::getStatus, SpellingClassPeriodEnum.NO_EXPIRED.getValue())
                .ne(SpellingClassPeriod::getClassAttendanceStatus, ScheduleDetailStatusEnum.CANCEL.getValue())
                .gt(SpellingClassPeriod::getCourseDate, currentDate)
                .orderByAsc(SpellingClassPeriod::getCourseDate));
        if (ParamUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 获取早于等于当前日期的课
     *
     * @param classId 班级id
     * @return SpellingClassPeriodEnum
     */
    public List<SpellingClassPeriod> getBeforeCurrentPeriod(String classId, Date currentDate, Date currentTime) {
        return this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, classId)
                .eq(SpellingClassPeriod::getStatus, "1")
                .le(SpellingClassPeriod::getCourseDate, currentDate)
                .le(SpellingClassPeriod::getEndTime, currentTime)
                .orderByAsc(SpellingClassPeriod::getCourseDate));
    }

    public Integer getTotalNum(String id) {
        return this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, id)).size();
    }

    public SpellingClassPeriod period(String classId, String id) {
        return this.getOne(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, classId)
                .eq(SpellingClassPeriod::getCourseScheduleDetailId, id));
    }

    public List<SpellingClassPeriod> periods(String classId, List<String> scheduleDetailIds) {
        return this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .eq(SpellingClassPeriod::getClassInfoId, classId)
                .in(SpellingClassPeriod::getCourseScheduleDetailId, scheduleDetailIds));
    }

    public List<SpellingClassPeriod> periods(List<String> scheduleDetailIds) {
        return this.list(new LambdaQueryWrapper<SpellingClassPeriod>()
                .in(SpellingClassPeriod::getCourseScheduleDetailId, scheduleDetailIds));
    }

    public List<CourseScheduleDetailVO> listByClassId(String classId) {
        return this.spellingClassPeriodMapper.listByClassId(classId, ScheduleDetailStatusEnum.CANCEL.getValue());
    }

    public List<SpellingClassPeriod> expireScheduleDetail(String currentDate, String currentTime, String value) {
        return this.spellingClassPeriodMapper.expireScheduleDetail(currentDate, currentTime, value);
    }

    /**
     * 更新状态
     *
     * @param classInfo 类信息
     */
    public void updateSpellClassPeriodStatus(ClassInfo classInfo, String optBy, Instant now) {
        LambdaQueryWrapper<SpellingClassPeriod> scpWrap = new LambdaQueryWrapper<>();
        scpWrap.eq(SpellingClassPeriod::getClassInfoId, classInfo.getId());
        List<SpellingClassPeriod> spellingClassPeriodList = this.list(scpWrap);
        if (ParamUtils.isNotEmpty(spellingClassPeriodList)) {
            for (SpellingClassPeriod spellingClassPeriod : spellingClassPeriodList) {
                if (spellingClassPeriod.getClassAttendanceStatus().equals(ScheduleDetailStatusEnum.WAIT_ATTENDANCE.getValue())) {
                    spellingClassPeriod.setDueNum(0);
                }
                if (ParamUtils.isNotEmpty(optBy)) {
                    spellingClassPeriod.setLastModifiedBy(optBy);
                    spellingClassPeriod.setLastModifiedDate(now);
                }
                spellingClassPeriod.setClassAttendanceStatus(ScheduleDetailStatusEnum.CANCEL.getValue());
                this.updateById(spellingClassPeriod);
            }
        }
    }


    /**
     * 根据班级id和考勤状态获取班级课表
     *
     * @param classId          班级id
     * @param attendanceStatus 考勤状态
     * @return List<SpellingClassPeriod>
     */
    public List<SpellingClassPeriod> periodByClassIdAndAttendanceStatus(List<String> classId, List<String> attendanceStatus, List<String> preDetailIds) {
        return this.spellingClassPeriodMapper.selectList(new LambdaQueryWrapper<SpellingClassPeriod>()
                .in(SpellingClassPeriod::getClassInfoId, classId)
                .in(SpellingClassPeriod::getClassAttendanceStatus, attendanceStatus)
                .in(SpellingClassPeriod::getCourseScheduleDetailId, preDetailIds));
    }

    public void cancelPeriod(String attendanceStatus, String courseScheduleDetailId, String classId, String cancelReason) {
        this.spellingClassPeriodMapper.cancelPeriod(attendanceStatus, courseScheduleDetailId, classId, cancelReason);
    }
}
